package com.sinog.auth.controller;

import com.sinog.core.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * @desc 浏览器下载
 * @author 王兴杰
 * @date 2021-01-22 11:57
 */
@Slf4j
@RestController
public class DownloadController {

    @Value("${gkzx.azbj.downloadfilepath.windows}")
    private String win;
    @Value("${gkzx.azbj.downloadfilepath.unix}")
    private String mac;
    private static final String ONE_STR = "1";
    private static final String WIN_STR = "win";

    /**
     * 下载chrome浏览器
     * @param request request
     * @param resp resp
     * @param type type
     */
    @RequestMapping("/upload/chrome/download")
    public void downloadChrome(HttpServletRequest request,HttpServletResponse resp,String type) {
        String fileUrls;
        if(ONE_STR.equals(type)) {
            fileUrls = "chrome_xp_49.0.2623.112.exe";
        } else {
            fileUrls = "chrome_85.0.4183.102.exe";
        }
        try {
            String os = System.getProperty("os.name");
            String path;
            if(os.toLowerCase().startsWith(WIN_STR)) {
                path = win;
            } else {
                path = mac;
            }
            FileUtils.downloadFile(request,resp,path + fileUrls,fileUrls);
        } catch(UnsupportedEncodingException e) {
            log.error("发生异常:{}",e.getMessage());
        }
    }
}